package com.example.demo.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * TODO
 *
 * @author caiyonglan
 * @email 627064518@qq.com
 * @date 2020/12/2 16:46
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
public class Knife4jConfiguration {

    private final OpenApiExtensionResolver openApiExtensionResolver;

    @Autowired
    public Knife4jConfiguration(OpenApiExtensionResolver openApiExtensionResolver) {
        this.openApiExtensionResolver = openApiExtensionResolver;
    }

    @Bean(value = "defaultApi")
    public Docket defaultApi() {
        Contact contact = new Contact("caiyonglan", "xxx", "627064518@qq.com");
        String groupName="v1.0版本接口";
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
            .host("https://www.baidu.com")
            .apiInfo(new ApiInfoBuilder()
                .title("Demo RESTful APIs")
                .description("# Demo RESTful APIs")
//                .termsOfServiceUrl("http://www.xx.com/")
                .contact(contact)
                .version("v1.0")
                .build())
            //分组名称
            .groupName(groupName)
            .select()
            //这里指定Controller扫描包路径
            .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
            .paths(PathSelectors.any())
            .build()
            .extensions(openApiExtensionResolver.buildSettingExtensions());
        return docket;
    }
}
